package com.mysimplenews.main.view;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.mysimplenews.R;
import com.mysimplenews.http.Netroid;
import com.mysimplenews.image.view.ImageFragment;
import com.mysimplenews.main.interfaces.MainViewInterface;
import com.mysimplenews.main.presenter.MainPresenterImp;
import com.mysimplenews.news.view.NewsFragment;
import com.mysimplenews.utils.FragmentContainerProcess;

public class MainActivity extends AppCompatActivity implements MainViewInterface{

    private Fragment newsFragment;
    private Fragment imagesFragment;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private NavigationView mNavigationView;
    private MainPresenterImp mainPresenter;


    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(MainActivity.this,"aaa====>" + item.getItemId(),Toast.LENGTH_LONG).show();
            return true;
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Netroid.init(this);
        setContentView(R.layout.activity_main);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setOnMenuItemClickListener(onMenuItemClick);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open,
                R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        setupDrawerContent(mNavigationView);
        mainPresenter = new MainPresenterImp(this);
        switch2News();
    }

    public void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                item.setChecked(true);
                mainPresenter.switchNavigation(item.getItemId());
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
    }


    @Override
    public void switch2News() {
        if(newsFragment == null)
            newsFragment = new NewsFragment();
        FragmentContainerProcess.getInstance(this).addOrShowFragment(newsFragment,R.id.frame_content);
    }

    @Override
    public void switch2Images() {
        if(imagesFragment == null)
            imagesFragment = new ImageFragment();
        FragmentContainerProcess.getInstance(this).addOrShowFragment(imagesFragment,R.id.frame_content);
    }

    @Override
    public void switch2Weather() {

    }

    @Override
    public void switch2About() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }else if(id == R.id.action_edit){
            return true;
        }else if(id == R.id.action_share){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
